package view;

import java.awt.GridBagLayout;
import javax.swing.JPanel;

import domain.ISalvable;
import domain.Partido;
import domain.Pronostico;
import domain.Quiniela;
import domain.QuinielaValidationException;

import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;

public class VPronostico extends JPanel implements ISalvable{

	private static final long serialVersionUID = 1L;

	protected Pronostico pronostico = null;
	protected VPairPartido1X2 [] rows = null;

	private VSave vSave = null;

	///TODO eliminar este constructor en cuanto terminemos las pruebas, no vale para nada
	public VPronostico() throws QuinielaValidationException {
		super();
		Partido [] partidos = new Partido[Quiniela.MAX_PARTIDOS_COL];
		for (int i=0; i<partidos.length; i++)
			partidos[i] = new Partido();
		pronostico =new Pronostico(new Quiniela(partidos));
		initialize();
	}

	public VPronostico(Pronostico p) {
		super();
		pronostico = p;
		initialize();
	}

	private void initialize() {
		//this.setSize(150, 200);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBorder(BorderFactory.createTitledBorder(null, "Pronostico", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		for (int i=0; i < getRows().length; i++)
			add(getRows()[i]);
		this.add(getVSave());

	}
	

	protected VPairPartido1X2[] getRows() {
		if (rows==null)
		{
			pronostico.getQuiniela();
			rows=new VPairPartido1X2[Quiniela.MAX_PARTIDOS_COL];
			for (int i=0; i<rows.length; i++)
			{
				rows[i]=new VPairPartido1X2(pronostico.getQuiniela().getPartidos()[i]);
			}
		}
		return rows;
	}

	/**
	 * This method initializes vSave	
	 * 	
	 * @return view.VSave	
	 */
	private VSave getVSave() {
		if (vSave == null) {
			vSave = new VSave(this);
		}
		return vSave;
	}

	@Override
	public void close() {
		///TODO implementar
		System.out.println("Cerrar pronostico");	}

	@Override
	public void save() throws Exception {
		///TODO implementar
		System.out.println("Salvado pronostico");
	}

}
